package com.china08.yxyapi.api.home.kecheng.sync;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.util.YxyKeChengUtil;

/**
 * 同步课程--详情页--观看视频页面
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengSyncWatchLessonVideoSrhController extends YxyApiBaseController {

    @Autowired
    private YxyKeChengUtil YxyKeChengUtil;

    // 同步课时
    @Autowired
    private YxyLessonsSynchroLessonItemRepository yxyLessonsSynchroLessonItemRepository;

    // 专题课时
    @Autowired
    private YxyLessonsTopicLessonItemRepository yxyLessonsTopicLessonItemRepository;

    // 校本课时
    @Autowired
    private YxyLessonsSchoolLessonItemRepository yxyLessonsSchoolLessonItemRepository;

    // @Autowired
    // private YxyResourceService yrs;

    @RequestMapping(value = "/api_yw/home/kecheng/sync/watch/lesson/video/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonItemId = reqModel.lessonItemId;
        String userId = getProfile(request, response).getId();

        RespModel model = new RespModel();

        String vipString = getProfile(request, response).getUserVip();
        String gerenVip = getProfile(request, response).getPerVip();
        Boolean userVip;
        if (vipString.equals("true") || gerenVip.equals("true")) {
            userVip = true;
        } else {
            userVip = false;
        }
        model.playUrl = YxyKeChengUtil.genLessonItemPlayUrl(lessonItemId, userId, userVip);

        YxyLessonsSynchroLessonItem synchroLessonItem = yxyLessonsSynchroLessonItemRepository.findById(lessonItemId);
        if (null != synchroLessonItem) {
            model.preview = synchroLessonItem.getPreview();
        }
        YxyLessonsTopicLessonItem topicLessonItem = yxyLessonsTopicLessonItemRepository.findById(lessonItemId);
        if (null != topicLessonItem) {
            model.preview = topicLessonItem.getPreview();
        }
        YxyLessonsSchoolLessonItem schoolLessonItem = yxyLessonsSchoolLessonItemRepository.findById(lessonItemId);
        if (null != schoolLessonItem) {
            model.preview = schoolLessonItem.getPreview();
        }
        return model;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {

        // 课时ID
        public String lessonItemId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 视频地址
        public String playUrl;

        // 是否可以免费试看 0--不可以试看, 1--可以试看
        public Integer preview;

    }
}
